<?php
/**
 * ----------------------------------------
 * @link http://www.ruaby.com/
 * @copyright Copyright (c) 2016 RuabySoft
 * @license http://www.ruaby.com/license/
 *
 * @author 18/8/30 下午6:01
 */

namespace Zeke\App\Core\Db;

class Db
{


    static private $instance;


    private $config;


    private $_db;

    /**
     * 获取单例
     *
     * @author <liu.bin@ruaby.com> 2018-08-30 18:29
     */
    public static function instance($config = [])
    {
        if (!self::$instance instanceof self) {
            self::$instance = new self($config);
        }
        return self::$instance;
    }



    private function db()
    {
        if($this->_db){
            return $this->_db;
        }

        $dbms   = 'mysql';
        $dbName = 'plm';
        $user   = 'root';
        $pwd    = '123456';
        $host   = '127.0.0.1';
        $dsn="$dbms:host=$host;dbname=$dbName";


        try{
            $pdo = new \PDO($dsn,$user,$pwd);
        }catch(\Exception $e){
            return $e->getMessage();
        }


        $this->_db = $pdo;
        return $this->_db;
    }


    /**
     * 查询数据
     *
     * @param $sql
     * @return array
     *
     * @author <liu.bin@ruaby.com> 2018-08-30 18:36
     */
    public function query($sql)
    {

        $statement  = $this->db()->query($sql);
        $rows       = $statement->FetchAll(\PDO::FETCH_ASSOC);
        $result     = [];
        //遍历
        foreach($rows as $row) {
            $result[] = $row;
        }

        return $result;
    }



    /**
     *
     * Db constructor.
     * @param $config
     */
    private function __construct($config=[])
    {
        $this->config = $config;
    }


    private function __clone()
    {
        // TODO: Implement __clone() method.
    }

}
